import java.awt.*;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.io.Serializable;
import java.util.*;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.plaf.ToolTipUI;

public class OthelloUI extends JFrame implements Serializable, Observer {
	// เธ•เธฒเธฃเธฒเธ� เธกเธดเธ�เธงเธฃเน€เธ�เธฅเธตเน�เธขเธ�
	private JLabel board[][];
	// เธ—เธตเน�เน�เธชเน�เธ•เธฒเธฃเธฒเธ�
	private JPanel bPanel;
	// เน€เธญเธฒเน�เธงเน�เธ—เธณเน�เธซเน�เธ•เธฒเธฃเธฒเธ�เธกเธตเธ�เธญเธ�เธ”เธณ
	private Border border;
	// เธ�เธ” labelเน�เธฅเน�เธงเธชเน�เธ�เน�เธ�เธ—เธตเน�server
	// เน�เธ�เน�
	private Client client;
	// เน�เธงเน�เน€เธ�เธ�เธ�เธดเธ�เธฑเธ”เธ�เธญเธ�labelเธ—เธตเน�เธ�เธฅเธดเธ�เน�เธงเน�เธชเน�เธ�เน�เธ�server
	private Message msg;
	// เน�เธกเน�เน�เธ�เน�เธกเธฑเน�เธ�
	private String color;
	// เน€เธ�เธกเธ�เธญเธ” เธ—เธตเน�เน€เธฃเธฒ observ
	private GameBoard gb;

	private Othello o;

	private OColor state;

	private List list;
	private JTextField text;
	private JPanel listp;
	private JPanel chatp;
	private JPanel pane;
	private JButton send;
	
	private JPanel east;
	private JPanel scorep;
	private JLabel turn;
	private JLabel bscore;
	private JLabel wscore;
	
	private ImageIcon makblack = new ImageIcon(OthelloUI.class.getResource("/image/ironmanblack.png"));
	private ImageIcon makwhite = new ImageIcon(OthelloUI.class.getResource("/image/ironman.png"));
	
	private ImageIcon header = new ImageIcon(OthelloUI.class.getResource("/image/java_othello_man.png"));
	private JPanel headp; 
	private JLabel headl;
	
	private JButton pass;


	public OthelloUI(Client client, Othello o) {
		//System.out.println("UI construct");
		this.client = client;
		this.o = o;
		this.gb = o.getGameBoard();
		client.addObserver(this);
		gb.addObserver(this);
		newComponent();
		initcomponent();
		afterAddGame();
		
	}

	// เธ�เธนเธญเธขเธฒเธ�add tooltipเธซเธฅเธฑเธ�เธ�เธฒเธ�เธฃเธฑเธ�gbเธกเธฒ เธ�เธฐเน�เธ”เน�เธ�เธญเธ�เธชเธตเน�เธ”เน�
	private void afterAddGame() {
		//System.out.println("-------------------------");
		createBoard();
		getContentPane().add(BorderLayout.NORTH,headp);
		getContentPane().add(BorderLayout.CENTER,bPanel);
		getContentPane().add(BorderLayout.SOUTH,pane);
		getContentPane().add(BorderLayout.EAST,east);
		
		paintBoard();
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		pack();
		//run();
	}

	// เน�เธงเน� เธชเธฃเน�เธฒเธ�component
	private void newComponent() {
		bPanel = new JPanel();
		bPanel.setLayout(new GridLayout(8, 8));
		send = new JButton("SEND");
		board = new JLabel[8][8];
		border = BorderFactory.createLineBorder(Color.BLACK);

		chatp = new JPanel(new FlowLayout());
		pane = new JPanel(new GridLayout(2, 1));
		text = new JTextField();
		listp = new JPanel();
		list = new List();
		
		east = new JPanel();
		scorep = new JPanel();
		turn = new JLabel();
		turn.setFont(new Font("Tahoma", Font.BOLD, 14));
		turn.setHorizontalAlignment(SwingConstants.CENTER);
		bscore = new JLabel();
		bscore.setFont(new Font("Tahoma", Font.BOLD, 14));
		bscore.setHorizontalAlignment(SwingConstants.CENTER);
		wscore = new JLabel();
		wscore.setFont(new Font("Tahoma", Font.BOLD, 14));
		wscore.setHorizontalAlignment(SwingConstants.CENTER);
		
		headp = new JPanel();
		headp.setBackground(Color.WHITE);
		headp.setPreferredSize(new Dimension(580,100));
		headl = new JLabel();
		
		pass = new JButton("PASS");
		pass.setFont(new Font("Tahoma", Font.BOLD, 12));
	}

	void run() {
		setVisible(true);
	}

	private void initcomponent(){
		//listp.add();
		chatp.setLayout(new FlowLayout());
		text.setPreferredSize(new Dimension(550,20));
		list.setPreferredSize(new Dimension(420,100));
		chatp.add(text);
		chatp.add(send);
		send.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {
					client.sendToServer("Gu "+color+"say: "+text.getText());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				text.setText(null);

			}
		});
		text.addKeyListener(new KeyListener() {

			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void keyReleased(KeyEvent e) {
				if(e.getKeyCode()==KeyEvent.VK_ENTER){
					try {
						client.sendToServer("Gu "+color+"say: "+text.getText());
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					text.setText(null);
				}

			}

			@Override
			public void keyPressed(KeyEvent e) {

			}
		});
		
		pass.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					client.sendToServer('p');
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		
		pane.add(list);
		pane.add(chatp);
		
		east.setLayout(new GridLayout(2, 1));
		scorep.setLayout(new GridLayout(4, 1));
		scorep.setPreferredSize(new Dimension(120, 50));
		scorep.add(turn);
		scorep.add(bscore);
		scorep.add(wscore);
		scorep.add(pass);
		east.add(scorep);
		
		headl.setIcon(header);
		headp.setBorder(BorderFactory
				.createLineBorder(Color.GRAY));
		headp.add(headl);
		
		
		
	}

	// เธชเธฃเน�เธฒเธ�เธ•เธฒเธฃเธฒเธ� เธญเธขเน�เธฒเธ—เธณเน€เธ�เน�เธ�เธ�เธฐ
	private void createBoard() {
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				board[i][j] = new JLabel();
				board[i][j].setBorder(BorderFactory
						.createLineBorder(Color.GRAY));
				board[i][j].setBackground(Color.WHITE);
				board[i][j].setPreferredSize(new Dimension(70, 70));
				board[i][j].toString();
				board[i][j].setOpaque(true);
				board[i][j].addMouseListener(new BoardListener(i, j));
				bPanel.add(board[i][j]);	
			}	
		}			//JOptionPane.showMessageDialog(null, "finished creating board");

	}

	private void paintBoard(){
		int b = 0;
		int w = 0;
		turn.setText("TURN : "+gb.getState());
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				if (gb.getColor(new Point(i, j)).equals(OColor.BLACK)) {
					board[i][j].setIcon(null);
					board[i][j].setIcon(makblack);
					b++;
				}
				if (gb.getColor(new Point(i, j)).equals(OColor.WHITE)) {
					board[i][j].setIcon(null);
					board[i][j].setIcon(makwhite);
					w++;
				}
				board[i][j].setToolTipText(i + "," + j + ","
						+ gb.getColor(new Point(i, j)));
			}
		}
		bscore.setText("BLACK: "+b);
		wscore.setText("WHITE: "+w);
	}

	// เธญเธ�เธดเธ�เธฒเธขเน�เธกเน�เธ–เธนเธ�
	public class BoardListener implements MouseListener {
		// เธ�เธดเธ�เธฑเธ”เธ�เธญเธ•เธฑเธงเธ—เธตเน�เน€เธฃเธฒเธ�เธ”
		private int j;
		private int i;

		public BoardListener(int i, int j) {
			this.i = i;
			this.j = j;
		}

		@Override
		public void mouseReleased(MouseEvent arg0) {
			if (state.equals(gb.getState())){
				// เธชเธฃเน�เธฒเธ�msgเน€เธ�เธทเน�เธญเธชเน�เธ�เน�เธ�serverเน€เธญเธฒเธ�เธดเธ�เธฑเธ”เธ�เธฒเธ�bl
				msg = new Message(i, j);
				try {

					//System.out.println(msg);
					client.sendToServer(msg);

				} catch (IOException e) {
					e.printStackTrace();
				}
				//System.out.println("send msg");
			}
		}

		@Override
		public void mousePressed(MouseEvent arg0) {

		}

		@Override
		public void mouseExited(MouseEvent arg0) {
		}

		@Override
		public void mouseEntered(MouseEvent arg0) {
		}

		@Override
		public void mouseClicked(MouseEvent arg0) {

		}
	}

	public void setState(String string) {

		System.out.println("state" + string);
		if (string.equals("black")){
			//System.out.println("set blakc state");
			state = gb.BLACK_TURN;
			color = "BLACK";

		}
		else if (string.equals("white")){
			//System.out.println("set white state");
			state = gb.WHITE_TURN;
			color = "WHITE";
		}
	}

	public OColor getUIState(){
		return state;
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		if (state == null){
			if(arg1 instanceof OColor){
				System.out.println("get OColor");
				if(arg1.equals(OColor.WHITE))
					setState("white");
				else if(arg1.equals(OColor.BLACK))
					setState("black");
			}
		}
		if (arg1 instanceof String){
			list.add((String) arg1);
			list.select(list.getItemCount() - 1);
		}
		if (arg1 instanceof Message){
			o.placeBut((Message) arg1);
		}
		if ( arg1 instanceof Character){
			gb.setState();
		}
		if (arg0 instanceof GameBoard) {
			paintBoard();
		}
		
	}
}
